package com.gopay.bis.manager.bank.ebank;

import java.util.Map;

import javax.annotation.Resource;

import com.gopay.bis.common.util.BankLogUtils;
import com.gopay.bis.common.util.BisPayCheckUtils;
import com.gopay.common.Result;
import com.gopay.common.constants.bank.BankConstants;
import com.gopay.common.domain.bank.BaseMessage;
import com.gopay.common.domain.bank.njcb.ebank.NjcbB2cMessage;
import com.gopay.common.domain.bank.njcb.ebank.query.request.NjcbeBankDataReq;
import com.gopay.common.domain.bank.njcb.ebank.query.request.OpReq;
import com.gopay.common.domain.bank.njcb.ebank.query.request.ReqParam;
import com.gopay.common.domain.bank.njcb.ebank.query.response.NjcbeBankDataRep;
import com.gopay.common.exception.GopayException;
import com.gopay.remote.bank.BankManagerRemoteService;
import com.gopay.remote.bank.businesses.BankService;

public class NjcbB2cManager implements BankManagerRemoteService {

	private static String SPLIT_FLAG="\\|\\|";
	
	@Resource(name = "njcbB2cBankServiceClient")
	private BankService njcbB2cBankServiceClient;
	
	@Resource(name = "njcbB2cBankServiceClientHna")
	private BankService njcbB2cBankServiceClientHna;
	
	@Resource(name = "njcbConfig")
	private Map<String, String> njcbConfig;

	@Override
	public Object query(Object obj) throws GopayException {
		NjcbeBankDataReq message = (NjcbeBankDataReq)obj;
		if(BisPayCheckUtils.checkExternalOrderID(message.getOrderId())){
			OpReq opReq = new OpReq();
			ReqParam reqParam = new ReqParam();
			reqParam.setOrderNo(message.getOrderId());
			reqParam.setStartDate(message.getOrderDate().substring(0, 8));
			reqParam.setEndDate(message.getOrderDate().substring(0, 8));
			opReq.setReqParam(reqParam);
			opReq.setReqTime(message.getOrderDate().substring(8));
			message.setOpReq(opReq);
			}
		message.getOpReq().getReqParam().setCustNo(message.getMerID());
		message.getOpReq().getReqParam().setOrderType(njcbConfig.get("NJCB.B2C.QUERY.ORDERTYPE"));
		BankLogUtils.printB2CManagerQuerySendLog(message, message);
		NjcbeBankDataRep orderInfo;
		if(message.isHna()){
			orderInfo = (NjcbeBankDataRep) njcbB2cBankServiceClientHna.query(message);
		}else{
			orderInfo = (NjcbeBankDataRep) njcbB2cBankServiceClient.query(message);
		}
		orderInfo.setOutStlmCd(BankConstants.BANK_LOGO_NJCB);
		BankLogUtils.printB2CManagerQueryReturnLog(orderInfo);
		return orderInfo;
	}

	@Override
	public Object sign(BaseMessage baseMessage) throws GopayException {
		NjcbB2cMessage message = (NjcbB2cMessage) baseMessage;
		buildMessage(message);
		buildPlain(message);
		BankLogUtils.printB2CManagerSignSendLog(message);
		String signMsg;
		if(message.isHna()){
			signMsg = (String) njcbB2cBankServiceClientHna.sign(message);
		}else{
		     signMsg = (String) njcbB2cBankServiceClient.sign(message);
		}
		String[] signArray = signMsg.split(SPLIT_FLAG);
		message.setSignMessage(signArray[0]);
		message.setKoalB64Cert(signArray[1]);
		BankLogUtils.printB2CManagerSignReturnLog(message);
		return message;
	}

	@Override
	public Object verify(BaseMessage baseMessage) throws GopayException {
		NjcbB2cMessage njcbMessage = (NjcbB2cMessage)baseMessage;
		BankLogUtils.printB2CManagerVerifySendLog(njcbMessage);
		boolean isVerify;
		if(njcbMessage.isHna()){
			isVerify = (Boolean) njcbB2cBankServiceClientHna.verify(baseMessage);
		}else{
			isVerify = (Boolean) njcbB2cBankServiceClient.verify(baseMessage);
		}
		BankLogUtils.printB2CManagerVerifyReturnLog(baseMessage, isVerify);
		return isVerify;
	}

	public void buildMessage(NjcbB2cMessage message) {
		message.setMerOrderAmt(message.getAmount().toString());
		message.setNetType((String) njcbConfig.get("NJCB.B2C.NETTYPE"));
		message.setMerchantID(message.getMerID());	
		message.setCurType((String) njcbConfig.get("NJCB.B2C.CURTYPE"));
		message.setMerTranType((String) njcbConfig.get("NJCB.B2C.MERTRANTYPE"));
		message.setMerNotifyUrl(message.getPayRuturnUrl());
		message.setMerNotifySign((String) njcbConfig.get("NJCB.B2C.MERNOTIFYSIGN"));
		message.setMerGetGoodsUrl(message.getPayNotifyUrl());
		message.setMerGetGoodsSign((String) njcbConfig.get("NJCB.B2C.MERGETGOODSSIGN"));
		message.setBankUrl(message.getPayUrl());
		message.setChannelType((String) njcbConfig.get("NJCB.B2C.CHANNELTYPE"));
		message.setMerOrderDate(message.getOrderDate().substring(0, 8));
		message.setMerOrderTime(message.getOrderDate().substring(8));
	}

	public void buildPlain(NjcbB2cMessage message) {
		StringBuffer sb = new StringBuffer();
		sb.append(message.getMerchantID()).append("|")
				.append(message.getOrderId()).append("|")
				.append(message.getMerOrderAmt()).append("|")
				.append(message.getCurType());
		message.setPlainMessage(sb.toString());
	}

	@Override
	public Object dayend(BaseMessage base) throws GopayException, Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Object refund(BaseMessage base) throws GopayException {
		// TODO Auto-generated method stub
		return null;
	}

}
